/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.job.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.dao.gen.bean.BizJob;
import com.smooth.site.sx.mag.biz.job.service.BizJobService;

/**
 * 职位Controller
 * @author bm
 * @version 2019-02-10
 */
@Controller
@RequestMapping(value = "${adminPath}/job/bizJob")
public class BizJobController extends BaseController {

	@Autowired
	private BizJobService bizJobService;
	
	@ModelAttribute
	public BizJob get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizJob entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizJobService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizJob();
		}
		return entity;
	}
	
	@RequiresPermissions("job:bizJob:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizJob bizJob, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizJob> page = bizJobService.findPage(new Page<BizJob>(request, response), bizJob); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/job/bizJobList";
		}
		return "biz/job/bizJobList";
	}
	
	@RequiresPermissions("job:bizJob:view")
	@RequestMapping(value = "form")
	public String form(BizJob bizJob, Model model) {
		model.addAttribute("bizJob", bizJob);
		return "biz/job/bizJobForm";
	}

	@RequiresPermissions("job:bizJob:edit")
	@RequestMapping(value = "save")
	public String save(BizJob bizJob, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizJob)){
			return form(bizJob, model);
		}
		try{
			bizJobService.save(bizJob);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/job/bizJob/?repage";
		}
		addMessage(redirectAttributes, "保存职位成功");
		return "redirect:"+Global.getAdminPath()+"/job/bizJob/?repage";
	}
	
	@RequiresPermissions("job:bizJob:edit")
	@RequestMapping(value = "delete")
	public String delete(BizJob bizJob, RedirectAttributes redirectAttributes) {
		try{
			bizJobService.delete(bizJob);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/job/bizJob/?repage";
		}
		addMessage(redirectAttributes, "删除职位成功");
		return "redirect:"+Global.getAdminPath()+"/job/bizJob/?repage";
	}

}